<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="vue.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .container {
      margin: 0 auto;
      width: 400px;
    }

    li {
      position: relative;
      list-style: none;
      width: 100%;
      height: 50px;
      border: 2px solid #333;
      border-collapse: collapse;
      line-height: 50px;
    }

    ul {
      width: 100%;
    }

    ul li button {
      position: absolute;
      width: 50px;
      height: 100%;
      right: 0;
      border: none;
    }

    ul .bar button {
      position: static;
      width: auto;
      padding: 10px;
    }

    .container input[type="text"] {
      width: 100%;
      height: 50px;
    }

    .todolist label {
      display: inline-block;
      width: 80%;
      height: 100%;
    }

    .todolist input[type="text"] {
      position: absolute;
      right: 0;
      top: 0;
      width: 90%;
    }

    label input {
      display: inline-block;
    }

    input[readonly] {
      cursor: default;
    }
  </style>
</head>

<body>
  <div id="app">
    <section class="container">
      <input type="text" v-model="msg" @keyup.enter="add">
      <div class="todolist">
        <ul v-if="list.length">
          <li v-for="(item,index) of listCpt">
            <input type="checkbox" v-model="item.status" name="checkbox" />

            <label for="checkbox" @dblclick="modify(item)">{{item.msg}}</label>
            <button @click="del(index)">x</button>
            <input type="text" v-show="item.editable" v-model="item.msg" @blur="item.editable=false" ref="inputVal" />
          </li>

        </ul>
        <footer class="bar" v-show="list.length">
          <button @click="mode=0">{{'all('+list.length+')'}}</button>
          <button @click="mode=1">active</button>
          <button @click="mode=2">completed</button>
        </footer>
      </div>
    </section>
  </div>

  <script>

    var vm = new Vue({
      el: '#app',
      data: {
        msg: '',
        mode: 0,
        list: []
      },
      computed: {
        listCpt() {
          return this.list.filter(
            item => {
              if (this.mode === 1) {
                return item.status === false
              }
              else if (this.mode === 2) {
                return item.status === true
              }
              else {
                return item
              }
            }
          )
        }
      },
      methods: {
        add() {//增
          if (this.msg) {
            this.list.push({ msg: this.msg, status: false, editable: false })
            this.msg = ''
          }
        },
        del(index) {//删
          this.list.splice(index, 1)
        },
        modify(item) {
          item.editable = true
          this.$nextTick(() => {
            this.$refs.inputVal[0].focus()
          })
        }

      }
    })
  </script>
</body>

</html>